package com.lm.jsqlparser.example;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;

public class JSQLParserExample {

    public static void main(String[] args) throws JSQLParserException {
        String sql = "SELECT id, name FROM users WHERE age > 18";

        // 解析 SQL
        Select selectStatement = (Select) CCJSqlParserUtil.parse(sql);
        PlainSelect plainSelect = (PlainSelect) selectStatement.getSelectBody();

        // 获取表名
        String tableName = plainSelect.getFromItem().toString();
        System.out.println("表名：" + tableName);

        // 获取字段
        plainSelect.getSelectItems().forEach(item -> System.out.println("字段：" + item));

        // 获取 WHERE 条件
        System.out.println("WHERE 条件：" + plainSelect.getWhere());
    }
}

